package com.scqkxx.gamebox.activity.space;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;

import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.CountCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.FunctionCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.myself.loginutils.OnRequestListener;
import com.scqkxx.gamebox.activity.space.adapter.SearcGoodsAdapter;
import com.scqkxx.gamebox.activity.space.adapter.SearcPostionAdapter;
import com.scqkxx.gamebox.activity.space.adapter.SearcSpacehAdapter;
import com.scqkxx.gamebox.databinding.ActivitySearchBinding;
import com.scqkzqtz.base.base.BaseActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SearchActivity extends BaseActivity<ActivitySearchBinding> {

    private List<AVObject> mSpaceListData;
    private List<AVObject> mPostionListData;
    private List<AVObject> mGoodsListData;

    private SearcSpacehAdapter mSearcSpacehAdapter;
    private SearcPostionAdapter mSearchPostionAdapter;
    private SearcGoodsAdapter mSearcGoodshAdapter;


    @Override
    public int setLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    public void init() {
        binding.mTitleView.setOnTitleClickListener((int titleType) -> finish());
        initView();
    }

    private void initView() {
        mSpaceListData = new ArrayList<>();
        mPostionListData = new ArrayList<>();
        mGoodsListData = new ArrayList<>();

        mSearcSpacehAdapter = new SearcSpacehAdapter(R.layout.seach_item_layout, mSpaceListData);
        mSearchPostionAdapter = new SearcPostionAdapter(R.layout.seach_item_layout, mPostionListData);
        mSearcGoodshAdapter = new SearcGoodsAdapter(R.layout.seach_item_layout, mGoodsListData);

        binding.mRecyclerViewSpace.setLayoutManager(new LinearLayoutManager(this));
        binding.mRecyclerViewPostion.setLayoutManager(new LinearLayoutManager(this));
        binding.mRecyclerViewGoods.setLayoutManager(new LinearLayoutManager(this));

        binding.mRecyclerViewSpace.setAdapter(mSearcSpacehAdapter);
        binding.mRecyclerViewPostion.setAdapter(mSearchPostionAdapter);
        binding.mRecyclerViewGoods.setAdapter(mSearcGoodshAdapter);

        binding.mRecyclerViewSpace.setNestedScrollingEnabled(false);
        binding.mRecyclerViewPostion.setNestedScrollingEnabled(false);
        binding.mRecyclerViewGoods.setNestedScrollingEnabled(false);

        //搜索监听
        binding.etSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //点击搜索的时候隐藏软键盘
                hideKeyboard(binding.etSearch);
                // 在这里写搜索的操作,一般都是网络请求数据
                String keyword = binding.etSearch.getText().toString().trim();
                searchData(keyword);
                return true;
            }
            return false;
        });
    }

    private void searchData(String keyword) {
        AVQuery<AVObject> query1 = AVQuery.getQuery("Space");
//        query1.whereContains("name", keyword);
        query1.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException avException) {
                Log.i("keyword", keyword + "");
                Log.i("search1", avObjects + "");
                if (avException == null) {
                    mSpaceListData.clear();
                    mSpaceListData.addAll(filterList(avObjects, keyword));
                    mSearcSpacehAdapter.notifyDataSetChanged();
                }

                binding.llSpace.setVisibility(mSpaceListData.size() == 0 ? View.GONE : View.VISIBLE);

                AVQuery<AVObject> query2 = AVQuery.getQuery("Position");
//                query2.whereContains("name", keyword);
                query2.findInBackground(new FindCallback<AVObject>() {
                    @Override
                    public void done(List<AVObject> avObjects, AVException avException) {
                        Log.i("search2", avObjects + "");
                        if (avException == null) {
                            mPostionListData.clear();
                            mPostionListData.addAll(filterList(avObjects, keyword));
                            mSearchPostionAdapter.notifyDataSetChanged();
                        }
                        binding.llPosition.setVisibility(mPostionListData.size() == 0 ? View.GONE : View.VISIBLE);

                        AVQuery<AVObject> query3 = AVQuery.getQuery("Goods");
//                        query3.whereContains("name", keyword);
                        query3.findInBackground(new FindCallback<AVObject>() {
                            @Override
                            public void done(List<AVObject> avObjects, AVException avException) {
                                Log.i("search3", avObjects + "");
                                if (avException == null) {
                                    mGoodsListData.clear();
                                    mGoodsListData.addAll(filterList(avObjects, keyword));
                                    mSearcGoodshAdapter.notifyDataSetChanged();
                                }
                                binding.llGoods.setVisibility(mGoodsListData.size() == 0 ? View.GONE : View.VISIBLE);

                                if (mSpaceListData.size() == 0 && mPostionListData.size() == 0 && mGoodsListData.size() == 0) {
                                    binding.scroll.setVisibility(View.GONE);
                                    binding.layoutEmpty.setVisibility(View.VISIBLE);
                                } else {
                                    binding.scroll.setVisibility(View.VISIBLE);
                                    binding.layoutEmpty.setVisibility(View.GONE);
                                }
                            }
                        });
                    }
                });
            }
        });

    }

    /**
     * 筛选列表
     *
     * @param dataList
     * @param keyword
     * @return
     */
    private List<AVObject> filterList(List<AVObject> dataList, String keyword) {
        List<AVObject> tempList = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            AVObject avObject = dataList.get(i);
            String name = avObject.getString("name");
            if (name != null) {
                Log.i("filterList", "name: " + name);
                if (name.contains(keyword) || name.indexOf(keyword) > 0) {
                    tempList.add(avObject);
                    Log.i("filterList", "contains: " + name);
                }
            }
        }

        Log.i("filterList", "tempList: " + tempList);
        return tempList;
    }

    /**
     * 隐藏软键盘
     *
     * @param view :一般为EditText
     */
    public void hideKeyboard(View view) {
        InputMethodManager manager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


}
